<?php


namespace app\api\controller\v13;


use app\common\model\LuckyPlanet;
use think\Cache;
use think\Db;

class Planet extends Auth2
{
    protected $total_day_max = 17500;// 总抽奖次数限制 0 代表不限制
    protected $user_day_max = 3;// 用户每日可抽奖次数上线 0 表示不限制
    protected $must_win = 3500;// 多少人中必中 0表示完全随机
    protected $time_interval = 14400;// 抽奖时间间隔 /s     0 表示无间隔时间
//    protected $time_limit_switch = false;// 是否开启时间段中奖 false表示此功能关闭  true 表示此功能开启
//    protected $time_limit = [];//中奖时间段 若需要使用此功能则 $time_limit_switch 必须为true才生效
    protected $rand_max = 5000;// 随机数最大值 与中奖率有关

    public function _initialize()
    {
        parent::_initialize();

        $this->init();
    }

    /**
     * 幸运星初始化
     */
    private function init()
    {
        $luckyPlanetConfig = config('luckyplanet');
        $this->total_day_max = $luckyPlanetConfig['total_day_max'];
        $this->user_day_max = $luckyPlanetConfig['user_day_max'];
        $this->must_win = $luckyPlanetConfig['must_win'];
        $this->time_interval = $luckyPlanetConfig['time_interval'];
        $this->total_day_max = $luckyPlanetConfig['total_day_max'];
        $this->rand_max = $luckyPlanetConfig['rand_max'];
        if ($this->rand_max < 12)
        {
            $this->rand_max = 19;
        }
    }

    public function index()
    {
        $uid = $this->user['id'] ?? null;

        $LuckyPlanet = new LuckyPlanet();

        // 获取当前抽奖总次数
        $total_count = $LuckyPlanet->getCountToday();
        // 获取用户今日抽奖情况
        if ($uid)
        {
            $user_today = $LuckyPlanet->getUserToday(['uid' => $uid]);
        } else
        {
            $user_today = [];
        }

        $user_surplus_times = 3;

        // 今日总抽奖次数已用尽
        if ($this->total_day_max > 0 && $total_count >= $this->total_day_max)
        {
            $user_surplus_times = 0;
        }

        if ($this->user_day_max > 0 && $user_surplus_times > 0)
        {
            $user_surplus_times = $this->user_day_max - count($user_today);
        }
        if ($this->request->time() < 1653926400)
        {
            // 关闭获取结果
            $winners = $LuckyPlanet->getWinToday(['uid' => $uid]);
        } else
        {
            // 返回虚拟数据
            $winnerDatas = Cache::get('luckyplanet:data:' . date('Ymd'), false);
            if (!$winnerDatas)
            {
                $index = random_int(1, 100);
                $data = Db::table('wechat_backup_07_18_14_43_00.zhunle_virtual_user_name')->alias('n')->join('wechat_backup_07_18_14_43_00.zhunle_virtual_user_avatar a', 'a.id=n.id')->limit($index, 3)->column('n.name,a.avatar', 'n.id');
                $w = [
                    '幸运金星',
                    '幸运火星',
                    '幸运木星',
                    '幸运土星',
                    '幸运水星',
                ];
                $d = [];
                foreach ($data as $item)
                {
                    $d[] = [
                        'name' => $item['name'],
                        'avatar' => $item['avatar'],
                        'prize' => $w[random_int(0, 4)],
                    ];
                }

                Cache::set('luckyplanet:data:' . date('Ymd'), $d, 86600);
                $winnerDatas = $d;
            }

            $winners = [];
            $h = date('H');
            foreach ($winnerDatas as $key => $winnerDataItem)
            {
                if (($key === 0 && $h >=7 ) || ($key === 1 && $h >= 13) || ($key === 2 && $h >= 19))
                {
                    $winners[] = [
                        "pid" => "0",
                        "uid" => "0",
                        "prize" => $winnerDataItem['prize'],
                        "nickname" => $winnerDataItem['name'],
                        "avatar" => $winnerDataItem['avatar'],
                        "status" => 1,
                        "isself" => 0
                    ];
                }
            }
        }

        if (empty($user_today))
        {
            $lattice = $this->user_day_max ? (1) : 0;
        } else
        {
            $lattice = $this->user_day_max ? ($user_today[0]['create_time']) : 0;
        }

        $data = [
            'list' => $winners,
            'times' => $user_surplus_times,
            'last_time' => $lattice,
        ];

        return show(config('code.success'), '加载成功', $data);
    }
}
